package com.elitel.frame.base.service.impl;

import com.elitel.common.elasticSearch.ESClient;
import com.elitel.common.ftp.FtpUtil;
import com.elitel.frame.base.component.exportprojectdata.DataBaseConnInfo;
import com.elitel.frame.base.component.exportprojectdata.ElasticSerachInfo;
import com.elitel.frame.base.service.SysDocumentService;
import com.elitel.frame.main.dao.SysDocumentMapper;
import com.elitel.frame.main.dao.SysPrjserviceMapper;
import com.elitel.frame.main.dao.ext.ExtSysDocumentMapper;
import com.elitel.frame.main.dao.ext.ExtSysPrjserviceMapper;
import com.elitel.frame.main.entity.CfgDb;
import com.elitel.frame.main.entity.SysDocument;
import com.elitel.frame.main.entity.SysPrjservice;
import com.elitel.frame.main.entity.ext.SysDocumentExt;
import com.elitel.frame.main.entity.vo.PageRequestVo;
import com.elitel.frame.main.entity.vo.PageResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 项目非结构化文档服务接口实现类
 * created by guoyanfei on 2018/4/18
 */
@Service
public class SysDocumentServiceImpl implements SysDocumentService {
    @Autowired
    private SysDocumentMapper sysDocumentMapper;
    @Autowired
    private ExtSysDocumentMapper extSysDocumentMapper;
    @Autowired
    private DataBaseConnInfo dataBaseConnInfo;
    @Autowired
    private ElasticSerachInfo elasticSerachInfo;
    @Autowired
    private SysPrjserviceMapper sysPrjserviceMapper;
    @Autowired
    private ExtSysPrjserviceMapper extSysPrjserviceMapper;

    /**
     * 验证ftp服务器是否联通
     * created on 2018/04/18
     * 实现方法
     * @param ftpHost FTP服务器地址
     * @param ftpPort FTP服务器端口号
     * @param ftpUser FTP服务器登录用户
     * @param ftpPass FTP服务器登录密码
     * @return
     */
    @Override
    public Boolean ftpConnection(String ftpHost, int ftpPort, String ftpUser, String ftpPass) {
        Boolean result = false;
        FtpUtil ftpUtil = new FtpUtil(ftpHost,ftpUser,ftpPass,ftpPort);
        result = ftpUtil.isconnection(ftpUser,ftpPass);
        ftpUtil.closeFtpClient();
        return result;
    }

    /**
     * 新增项目非结构化文档信息
     * created on 2018/04/18
     * 实现方法
     * @param sysDocument 实体
     * @return 新增条数
     * @author guoyanfei
     */
    @Override
    public Integer insertSysDocument(SysDocumentExt sysDocumentExt) {
//        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Integer result = 0;
        //1、新增文档检索服务信息表
        SysDocument sysDocument = new SysDocument();
        sysDocument.setGuid(sysDocumentExt.getGuid());
        sysDocument.setServicename(sysDocumentExt.getServicename());
        sysDocument.setFtphost(sysDocumentExt.getFtphost());
        sysDocument.setFtpport(sysDocumentExt.getFtpport());
        sysDocument.setFtpuser(sysDocumentExt.getFtpuser());
        sysDocument.setFtppass(sysDocumentExt.getFtppass());
        sysDocument.setDocpath(sysDocumentExt.getDocpath());
        sysDocument.setIndextype(sysDocumentExt.getIndextype());
        sysDocument.setStatus(sysDocumentExt.getStatus());
        sysDocument.setCronexpression(sysDocumentExt.getCronexpression());
        sysDocument.setRemark(sysDocumentExt.getRemark());
        sysDocument.setDocsource(sysDocumentExt.getDocsource());
        sysDocument.setCreatedate(new Date().toString());
        sysDocument.setCreateuser("admin");

        result += sysDocumentMapper.insertSelective(sysDocument);

        //2、新增工程信息表
        SysPrjservice sysPrjservice = new SysPrjservice();
        sysPrjservice.setGuid(UUID.randomUUID().toString());
        sysPrjservice.setPcode(sysDocumentExt.getPcode());
        sysPrjservice.setServicetype(sysDocumentExt.getServicetype());
        sysPrjservice.setServicecode(sysDocumentExt.getGuid());
        sysPrjservice.setCreatedate(new Date());
        result += sysPrjserviceMapper.insertSelective(sysPrjservice);

        return result;
    }

    /**
     * 修改项目非结构化文档信息
     * created on 2018/04/18
     * 实现方法
     * @param sysDocument 实体
     * @return 修改条数
     * @author guoyanfei
     */
    @Override
    public Integer updateSysDocument(SysDocument sysDocument) {
        return sysDocumentMapper.updateByPrimaryKeySelective(sysDocument);
    }

    /**
     * 根据主键ID删除非结构化文档配置信息
     * created on 2018/04/19
     * 实现方法
     * @param guid
     * @return 删除结果
     * @author guoyanfei
     */
    @Override
    public Integer deleteSysDocument(String guid,String procode) {
        Integer result = 0;
        result = sysDocumentMapper.deleteByPrimaryKey(guid);

        //elasticsearch索引库
        deleteESindex(procode);

        return result;
    }

    /**
     * 删除elasticsearch索引库数据
     * created on 2018/03/28
     * @param serviceid
     * @author guoyanfei
     */
    private void deleteESindex(String serviceid){
        ESClient esClient = new ESClient(elasticSerachInfo.address,
                elasticSerachInfo.port,elasticSerachInfo.clustername);
        esClient.deleteIndex(serviceid);
        esClient.closeClient();

    }


    /**
     * 查询非结构化文档配置信息<br/>
     * 如项目编码(procode)为空查询所有的信息，否则查询该项目编码对应的配置信息
     * created on 2018/04/19
     * 实现方法
     * @param serviceId 服务ID
     * @return SysDocument实体 list集合
     * @author guoyanfei
     */
    @Override
    public List<SysDocument> getSysDocumentListByserviceId(String serviceId) {
        return extSysDocumentMapper.getSysDocumentListByserviceId(serviceId);
    }

    /**
     * 查询文档检索服务ID是否存在
     * created on 2018/05/23
     * 实现方法
     * @param serviceId 服务ID
     * @return 查询条数
     * @author guoyanfei
     */
    @Override
    public Integer isExitserviceId(String serviceId) {
        Integer result = 0;
        SysDocument sysDocument = sysDocumentMapper.selectByPrimaryKey(serviceId);
        if(sysDocument != null && !sysDocument.equals("")){
            result = 1;
        }
        return result;
    }

    /**
     * 通过文档服务ID查询文档检索服务配置信息
     * created on 2018/05/23
     * 实现方法
     * @param serviceId 文档服务Id
     * @return SysDocument实体
     * @author guoyanfei
     */
    @Override
    public SysDocument selectInfoByserviceId(String serviceId) {
        return sysDocumentMapper.selectByPrimaryKey(serviceId);
    }

    /**
     * 删除服务中间表(SYS_PRJSERVICE),删除文档检索配置信息表(SYS_DOCUMENT)<br/>
     * 删除elasticsearch索引库
     * created on 2018/05/23
     * 实现方法
     * @param sysPrjservice 实体
     * @return 删除结果
     * @author guoyanfei
     */
    @Override
    public Integer deleteDocInfoByprjservices(SysPrjservice sysPrjservice) {
        Integer result = 0;

        //删除中间表
        result += sysPrjserviceMapper.deleteByPrimaryKey(sysPrjservice.getGuid());
        //删除文档检索配置信息表
        result += sysDocumentMapper.deleteByPrimaryKey(sysPrjservice.getServicecode());
        //elasticsearch索引库
        deleteESindex(sysPrjservice.getServicecode());

        return result;
    }

    /**
     * 通过服务ID查询服务与项目关联信息
     * created on 2018/05/24
     * 实现方法
     * @param serviceId 服务ID
     * @return SysPrjservice 实体
     * @author guoyanfei
     */
    @Override
    public SysPrjservice getSysPrjserviceByid(String serviceId) {
        return extSysPrjserviceMapper.getProjectServiceByServiceCode(serviceId);
    }
}
